package com.zrar.ak.gkpt.service.impl.manage;

import com.zrar.ak.gkpt.dao.IManageUnitInfoDao;
import com.zrar.ak.gkpt.model.bo.ManageUnitInfoBO;
import com.zrar.ak.gkpt.model.vo.ManageUnitInfoVO;
import com.zrar.ak.gkpt.model.vo.UserVO;
import com.zrar.ak.gkpt.service.manage.IManageUnitInfoService;
import com.zrar.ak.gkpt.util.BusinessEnum;
import com.zrar.ak.gkpt.util.CommonTools;
import com.zrar.ak.gkpt.util.JsonResult;
import com.zrar.ak.gkpt.util.JsonResultEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: ManageUnitInfoService
 * @Description 单位管理Service层
 * @Author liyang
 * @Time 2020/04/03
 */
@Service
@Slf4j
public class ManageUnitInfoService implements IManageUnitInfoService {

    @Autowired
    private IManageUnitInfoDao manageUnitInfoDao;

    @Override
    public JsonResult getLowerUnitAccountInfoList(JsonResult jsonResult, String unitName) {
        if(StringUtils.isEmpty(unitName)){
            unitName = null;
        }
        Map map = new HashMap();
        map.put("unitName",unitName);
        List<ManageUnitInfoVO> list = manageUnitInfoDao.getLowerUnitAccountInfoList(map);
        jsonResult.setData(list);
        if(list!=null && list.size()>0){
            CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_SUCCESS);
        }else{
            CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_NONEXISTENT_DATA);
        }
        return jsonResult;
    }

    @Override
    public JsonResult getLowerUnitRecordInfoList(JsonResult jsonResult, String unitName) {
        if(StringUtils.isEmpty(unitName)){
            unitName = null;
        }
        Map map = new HashMap();
        map.put("unitName",unitName);
        List<ManageUnitInfoVO> list = manageUnitInfoDao.getLowerUnitRecordInfoList(map);
        jsonResult.setData(list);
        if(list!=null && list.size()>0){
            CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_SUCCESS);
        }else{
            CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_NONEXISTENT_DATA);
        }
        return jsonResult;
    }

    @Override
    public String getUnitNameByUnitId(String unitId) {
        ManageUnitInfoBO manageUnitInfoBO = manageUnitInfoDao.selectByPrimaryKey(unitId);
        return manageUnitInfoBO.getUnitName();
    }

    @Override
    public JsonResult dataEdit(JsonResult jsonResult, String formDataJson, UserVO userVO) {
        try {
            ManageUnitInfoBO manageUnitInfoBO = CommonTools.jsonStrToObject(formDataJson, ManageUnitInfoBO.class);
            String updateUserId = userVO.getUserId();
            Date updateTime = new Date();
            manageUnitInfoBO.setUpdateUserId(updateUserId);
            manageUnitInfoBO.setUpdateTime(updateTime);
            if(manageUnitInfoBO.getIsDelete() == null){
                //如果页面不传，默认为有效数据，如果页面传入该字段的值，则默认执行该操作
                manageUnitInfoBO.setIsDelete(BusinessEnum.IS_DELETE_N.getValue());
            }
            if(StringUtils.isEmpty(manageUnitInfoBO.getId())){
                //新增操作
                manageUnitInfoBO.setId(CommonTools.getUUID());
                manageUnitInfoBO.setCreatorId(updateUserId);
                manageUnitInfoBO.setCreateTime(updateTime);
                manageUnitInfoDao.insert(manageUnitInfoBO);
            }else if(BusinessEnum.IS_DELETE_Y.getValue().equals(manageUnitInfoBO.getIsDelete())){
                //逻辑删除
                manageUnitInfoDao.updateByPrimaryKeySelective(manageUnitInfoBO);
            }else{
                //修改操作(非逻辑删除)
                ManageUnitInfoBO unitInfo = manageUnitInfoDao.selectByPrimaryKey(manageUnitInfoBO.getId());
                manageUnitInfoBO.setCreatorId(unitInfo.getCreatorId());
                manageUnitInfoBO.setCreateTime(unitInfo.getCreateTime());
                manageUnitInfoDao.updateByPrimaryKey(manageUnitInfoBO);
            }
            CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
            CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_BUSINESS_EXCEPTION);
        }
        return jsonResult;
    }

    @Override
    public JsonResult dataGet(JsonResult jsonResult, String unitId) {
        if(StringUtils.isEmpty(unitId)){
            ManageUnitInfoBO manageUnitInfoBO = new ManageUnitInfoBO();
            manageUnitInfoBO.setIsDelete(BusinessEnum.IS_DELETE_N.getValue());
            List<ManageUnitInfoBO> dataList = manageUnitInfoDao.select(manageUnitInfoBO);
            jsonResult.setData(dataList);
        }else{
            ManageUnitInfoBO manageUnitInfoBO = manageUnitInfoDao.selectByPrimaryKey(unitId);
            jsonResult.setData(manageUnitInfoBO);
        }
        CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_SUCCESS);
        return jsonResult;
    }
}
